package com.example.testdemo.douyin;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 仿抖音录制按钮
 * Created by zhangwenxiang on 2018/7/24.
 */

public class OldRecordVideoView extends View{
    private static final String TAG = "RecordVideoView";
    private Paint outPaint;//外圆画笔
    private Paint inPaint;//内圆画笔

    private int recordState;//录制状态 0.未录制 1.录制种 2.录制完成
    private float mXCenter;//x中心点 宽度配置需固定 //  private float mYCenter;//y中心点
    private float mRadius;//半径
    private float mStroke = 20;//圆环粗细
    private float sLeft, sTop, sRight, sBottom;
    //圆动画
    private float animStroke = 30;//动画圆环 默认大小
    private float minStorke = 20;
    private float maxSotrke = 40;
    private boolean isAnimSub = true;//是否动画缩小

    public OldRecordVideoView(Context context) {
        super(context);
        initPaint();
    }
    public OldRecordVideoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }
    private void initPaint(){
        //外圆（动画）
        outPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        outPaint.setColor(Color.parseColor("#FF4A74"));
        outPaint.setStyle(Paint.Style.STROKE);
        outPaint.setAlpha(90);
        //内圆（方形）
        inPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        inPaint.setColor(Color.parseColor("#FF4A74"));
    }

    private float xNum;
    private float xNum2;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mXCenter = getWidth() / 2;//x中心点    :mYCenter = getHeight() / 2;//y中心点
        if(recordState == 0){//未录制 (静止)
            //画外环
            mRadius = mXCenter - mStroke * 2.5f;//外圆半径
            outPaint.setStrokeWidth(20);//描边
            canvas.drawCircle(mXCenter, mXCenter, mRadius , outPaint);
            //画内圆
            canvas.drawCircle(mXCenter, mXCenter, mRadius - 15, inPaint);
        }else if(recordState == 1){//录制中 （启动动画）
            //方形位置
//            sLeft = sTop =  mXCenter - (mXCenter/3);
//            sRight = sBottom = mXCenter + (mXCenter/3);
            float minSqRate = mXCenter / 3;
            float defSqRate = mXCenter / 2 - xNum;
            if(defSqRate > minSqRate){
                xNum += 1.5;
            }
            Log.i(TAG, "minSqRate="+minSqRate+" sqRadius="+defSqRate);
            sLeft = sTop =  mXCenter - (defSqRate);
            sRight = sBottom = mXCenter + (defSqRate);
            //画方形
            RectF rect = new RectF(sLeft, sTop, sRight, sBottom);
            canvas.drawRoundRect(rect, 10, 10, inPaint);

            //画动画外圆
            mRadius = mXCenter - 10;//动画圆半径
            //计算边框
            if(animStroke > minStorke && isAnimSub){
                animStroke -= 0.5;
                if(animStroke == minStorke)isAnimSub = false;
            }else if(animStroke < maxSotrke && !isAnimSub){
                animStroke += 0.5;
                if(animStroke == maxSotrke)isAnimSub = true;
            }
            Log.i(TAG, "STROKE="+animStroke);
            //计算半径
            mRadius = mRadius - animStroke/2;
            //描边
            outPaint.setStrokeWidth(animStroke);
            canvas.drawCircle(mXCenter, mXCenter, mRadius , outPaint);

            postInvalidate();
        }else {//录制完成
            float outRadius = mXCenter - mStroke * 2.5f;//外圆半径
            float inRadius = outRadius - 15;//内圆半径

            float nowInRadius = (sBottom - sTop) / 2 + xNum;
            if(nowInRadius < inRadius){
                xNum+=1.5;
            }
            //画内圆
            canvas.drawCircle(mXCenter, mXCenter, nowInRadius, inPaint);

            float nowOutRadius = mRadius + xNum2;
            if(nowOutRadius > outRadius){
                xNum2--;
            }
            outPaint.setStrokeWidth(20);//描边
            canvas.drawCircle(mXCenter, mXCenter, nowOutRadius , outPaint);

            if(nowInRadius < inRadius || nowOutRadius > outRadius){
                postInvalidate();
            }
        }
    }


    /**
     * 开始录制
     */
    public void onStart(){
        if(recordState == 1){
            return;
        }
        xNum = xNum2 = 0;
        recordState = 1;
        postInvalidate();
    }

    /**
     * 停止录制（中间停止录制）
     */
    public void onStop(){
        if(recordState != 1){
            return;
        }
        xNum = xNum2 = 0;
        recordState = 0;
        postInvalidate();
    }

    /**
     * 录制完成
     */
    public void onSuccess(){
        if(recordState == 2){
            return;
        }
        xNum = xNum2 = 0;
        recordState = 2;
        postInvalidate();
    }
}
